﻿using System;
using System.Drawing;

namespace CS_Discovery
{
    /// <summary>
    /// 验证码生成器
    /// </summary>
    [Serializable()]
    public class ValidateImageGenerater
    {
        /// <summary>
        /// 初始化对象
        /// </summary>
        public ValidateImageGenerater()
        {
        }

        private int intTextLength = 6;
        /// <summary>
        /// 生成的验证码文本长度
        /// </summary>
        public int TextLength
        {
            get
            {
                return intTextLength;
            }
            set
            {
                intTextLength = value;
            }
        }

        private string strText = null;
        /// <summary>
        /// 验证码文本值
        /// </summary>
        public string Text
        {
            get
            {
                return strText;
            }
            set
            {
                strText = value;
            }
        }

        private double dblNoiseRate = 0.3;
        /// <summary>
        /// 噪声比例,也就是图片中某个像素设置为杂色点的概率，取值从0.0到1.0之间
        /// </summary>
        public double NoiseRate
        {
            get
            {
                return dblNoiseRate;
            }
            set
            {
                dblNoiseRate = value;
            }
        }
        /// <summary>
        /// 判断指定的文本是否等于验证码
        /// </summary>
        /// <param name="txt">要检查的文本</param>
        /// <returns>是否通过验证</returns>
        public bool Check(string txt)
        {
            return strText == txt;
        }

        /// <summary>
        /// 重新生成验证码文本内容
        /// </summary>
        public void Generate()
        {
            // 创建一个包含随机内容的验证码文本
            System.Random rand = new Random();
            char[] chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray();
            System.Text.StringBuilder myStr = new System.Text.StringBuilder();
            for (int iCount = 0; iCount < intTextLength; iCount++)
            {
                myStr.Append(chars[rand.Next(chars.Length)]);
            }
            strText = myStr.ToString();
        }

        /// <summary>
        /// 生成包含验证码内容的位图对象
        /// </summary>
        /// <returns>生成的位图对象</returns>
        public System.Drawing.Bitmap CreateBitmap()
        {
            Size ImageSize = Size.Empty;
            Font myFont = new Font("MS Sans Serif", 20);

            // 计算验证码图片大小
            using (Bitmap bmpTemp = new Bitmap(1, 1))
            {
                using (Graphics g = Graphics.FromImage(bmpTemp))
                {
                    SizeF size = g.MeasureString(strText, myFont, 10000);
                    ImageSize.Width = (int)size.Width + 8;
                    ImageSize.Height = (int)size.Height + 8;
                }
            }

            // 创建验证码图片
            Bitmap bmp = new Bitmap(ImageSize.Width, ImageSize.Height);
            // 绘制验证码文本
            using (Graphics g = Graphics.FromImage(bmp))
            {
                // 使用白色清空画布
                g.Clear(Color.White);
                using (StringFormat f = new StringFormat())
                {
                    // 水平左对齐
                    f.Alignment = StringAlignment.Near;
                    // 垂直居中对齐
                    f.LineAlignment = StringAlignment.Center;
                    // 不换行
                    f.FormatFlags = StringFormatFlags.NoWrap;
                    g.DrawString(
                        strText,
                        myFont,
                        Brushes.Black,
                        new RectangleF(
                        0,
                        0,
                        ImageSize.Width,
                        ImageSize.Height),
                        f);
                }//using
            }//using

            // 制造噪声 每个像素点设置为杂色点的概率等于 NoiseRate 属性指定
            // 的值，默认为0.3，也就是整个图片杂色点总面积占图片面积的 30%
            System.Random rand = new Random();
            for (int x = 0; x < ImageSize.Width; x++)
            {
                for (int y = 0; y < ImageSize.Height; y++)
                {
                    if (rand.NextDouble() <= this.NoiseRate)
                    {
                        // 计算随机像素值
                        int r = rand.Next(255);
                        int g = rand.Next(255);
                        int b = rand.Next(255);
                        Color c = Color.FromArgb(r, g, b);
                        // 设置位图中的像素值
                        bmp.SetPixel(x, y, c);
                    }
                }//for
            }//for

            myFont.Dispose();

            return bmp;
        }//public System.Drawing.Bitmap CreateBitmap()
    }//public class ValidateImageBuilder
}